package netbayServlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import util.String.StringUtil;
import util.jQgrid.JQGridFormatterUtil;

import netbay.NetbayUtility;
import netbay.TransactionManager;

/**
 * Servlet implementation class RDBilling
 */
public class RDBilling extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static String ratePerTransactionRd;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RDBilling() {
        super();
        // TODO Auto-generated constructor stub
    }
    public void init() {
		String configPath = getServletContext().getRealPath(
				"/WEB-INF/config.properties");
		NetbayUtility.init(configPath);
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as // 8-bit Unicode (e.g., for Czech characters)
        response.setContentType("text/json;charset=utf-8");
        HttpSession session = request.getSession();
        String url = "";
        String userId = ((!StringUtil.isNullOrEmpty((String)(session.getAttribute("userId")))) ? (String)session.getAttribute("userId"): "null");
        if(!(userId.equals("null"))){
        	url = "/RDBilling.jsp";
        	request.setAttribute("ratePerTransactionRd",ratePerTransactionRd);
        }
       
        else{
        	url = "/Login.jsp";
        }
        try{
        	request.getRequestDispatcher(url).forward(request, response);
		} catch (Exception ex) {
	        ex.printStackTrace();
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		 response.setContentType("text/json;charset=utf-8");
		 PrintWriter out = response.getWriter();
	     String strJson = "{}";
	     String userPath = request.getServletPath();
	     
	     if (userPath.equals("/RDBilling/BindGrdRDBilling")) {
	    	 String page = request.getParameter("page"); // get the requested page
	         String limit = request.getParameter("rows"); // get how many rows we want to have into the grid
	         String sidx = request.getParameter("sidx"); // get index row - i.e. user click to sort
	         String sord = request.getParameter("sord"); // get the direction
	         
	         String fromTime = (!StringUtil.isNullOrEmpty(request.getParameter("fromTime"))) ? request.getParameter("fromTime").trim() : "";
	         String toTime = (!StringUtil.isNullOrEmpty(request.getParameter("toTime"))) ? request.getParameter("toTime").trim() : "";
	         
	         try {
		         fromTime = dateToDecimal(fromTime,"from");
				 toTime = dateToDecimal(toTime,"to");
				 
				 List result = null;
				 Integer all = 0;
				 
				 TransactionManager tm = new TransactionManager();
				 
				 all = tm.getDisplayBilling(fromTime,toTime,"RD");
				 result = tm.getDisplayBilling(fromTime,toTime,"RD",page,limit,sidx,sord);

				 strJson = JQGridFormatterUtil.getJSON(Integer.parseInt(page), Integer.parseInt(limit), all, result, all);
				//out.println(strJson);
	         } catch (Exception ex) {
	        	 strJson = "{\"error\": \"" + ex.getMessage() + "\"}" ;
	         }
	         out.println(strJson);
	     }
	}
	
	public String dateToDecimal (String date,String type) throws Exception{
		 String[] splitDate = date.split("[-]");
		 String dateToDecimal = "";
		 for(int i =0;i<splitDate.length;i++){
			 dateToDecimal = dateToDecimal+splitDate[i];
		 }
		 
		 if(type.equals("from"))dateToDecimal = dateToDecimal+"000000.000";
		 if(type.equals("to"))dateToDecimal = dateToDecimal+"999999.999";
		 return dateToDecimal;
	}
}